package randomDB;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Random;

public class RandomValuesDB {

	public static void main(String[] args) {

		Date d1=new Date(108,0,1);	
		Statement s=null;
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Connection c=null;
		String dbUrl = "jdbc:odbc:orcl"; //change to my DB SID
	    String user = "system"; // change to my DB User
	    String password = "d3tatox99"; // change to my DB password
	    try{
	    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    }
	    catch(ClassNotFoundException e) {
	    e.printStackTrace();
	    System.out.println("Eroare incarcare driver!\n" + e);
	    }
	    try {
			c=DriverManager.getConnection(dbUrl, user, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			s= c.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		try {
			s.execute("Drop table data");
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		System.out.println("Starting!");
		try {
			s.execute("CREATE TABLE Data (VALUE NUMBER(4), TIME DATE, PRIMARY KEY (TIME) VALIDATE ) ORGANIZATION INDEX TABLESPACE SYSTEM PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE ( INITIAL 64K FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) LOGGING");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Random generator = new Random();
		int number=0;
		int fin=0;
		int i=1;
		int insert=0;
		int j;
		while(i<2000000){
			number=generator.nextInt(8800);

			j=0;
			if (fin<number)
		    while(fin<number){
		    	j++;
		    d1.setSeconds(d1.getSeconds()+1+generator.nextInt(200));
		    fin=fin+j+generator.nextInt(100);
		    String UpdateString="INSERT INTO Data(VALUE,TIME) VALUES("+fin+", to_date('"+df.format(d1)+"','yyyy-mm-dd hh24:mi:ss'))";		    
			try {
				s.executeUpdate(UpdateString);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int v=generator.nextInt(100);
			for(int k=0;k<v;k++){
				insert=fin+generator.nextInt(50);
				d1.setSeconds(d1.getSeconds()+1+generator.nextInt(15));
				 UpdateString="INSERT INTO Data(VALUE,TIME) VALUES("+insert+", to_date('"+df.format(d1)+"','yyyy-mm-dd hh24:mi:ss'))";
		    
			try {
				s.executeUpdate(UpdateString);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}else
			 while(fin>number){
			    	j++;
			    d1.setSeconds(d1.getSeconds()+1+generator.nextInt(150));
			    fin=fin-j-generator.nextInt(10);
			    String UpdateString="INSERT INTO Data(VALUE,TIME) VALUES("+fin+", to_date('"+df.format(d1)+"','yyyy-mm-dd hh24:mi:ss'))";
			    
				try {
					s.executeUpdate(UpdateString);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int v=generator.nextInt(100);
				for(int k=0;k<v;k++){
					insert=fin+generator.nextInt(50);
					d1.setSeconds(d1.getSeconds()+1+generator.nextInt(15));
					UpdateString="INSERT INTO Data(VALUE,TIME) VALUES("+insert+", to_date('"+df.format(d1)+"','yyyy-mm-dd hh24:mi:ss'))";
			    
				try {
					s.executeUpdate(UpdateString);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			 }
			int v=generator.nextInt(1000);
			for(int k=0;k<v;k++){
				insert=fin+generator.nextInt(100);
				d1.setSeconds(d1.getSeconds()+1+generator.nextInt(15));
				 String UpdateString="INSERT INTO Data(VALUE,TIME) VALUES("+insert+", to_date('"+df.format(d1)+"','yyyy-mm-dd hh24:mi:ss'))";
		    
			try {
				s.executeUpdate(UpdateString);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		i++;
		    
		}

	   
	}
	
}
